# 导包
import json,requests
import logging,app
from logging import handlers

from bs4 import BeautifulSoup
from htmltestreport import HTMLTestReport

# 创建类
def init_log_info():

    # 创建日志器
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)

    # 创建控制台处理器
    sh = logging.StreamHandler()

    # 创建文件处理器
    log_path = app.BASE_DIR + '/log/p2p.log'
    fh = logging.handlers.TimedRotatingFileHandler(log_path, when='M', interval=2, backupCount=5, encoding='utf-8')

    # 创建格式化器
    f = '%(asctime)s %(levelname)s [%(name)s] [%(filename)s(%(funcName)s:%(lineno)d)] - %(message)s'
    formatter = logging.Formatter(f)

    # 把格式化器添加到处理器
    sh.setFormatter(formatter)
    fh.setFormatter(formatter)

    # 把处理器添加到日志器
    logger.addHandler(sh)
    logger.addHandler(fh)


def assert_util(self, response, status_code, status, description):
    self.assertEqual(status_code, response.status_code)
    self.assertEqual(status, response.json().get("status"))
    self.assertEqual(description, response.json().get("description"))


# 读取图片验证码参数化文件
def read_file_img():
    test_case_list = []
    img_path = app.BASE_DIR + '/data/imgCode.json'
    with open(img_path, encoding='utf-8') as f:
        test_case = json.load(f)
    for i in test_case:
        type = i.get('type')
        status_code = i.get('status_code')
        test_case_list.append((type, status_code))
    print(test_case_list)
    return test_case_list


# 读取注册的参数化文件
def read_register_file():
    test_case_list = []
    file_path = app.BASE_DIR + '/data/register.json'
    with open(file_path, encoding='utf-8') as f:
        test_data = json.load(f)
    for i in test_data:
        phone = i.get("phone")
        password = i.get("password")
        verifycode = i.get("verifycode")
        phone_code = i.get("phone_code")
        dy_server = i.get("dy_server")
        invite_phone = i.get("invite_phone")
        status_code = i.get("status_code")
        status = i.get("status")
        desc = i.get("desc")
        test_case_list.append((phone, password, verifycode, phone_code, dy_server, invite_phone, status_code, status, desc))
    return test_case_list


# 公共读取文件方法
def read_common_file(filename):
    test_case_list = []
    file_path = app.BASE_DIR + "/data/" + filename
    with open(file_path, encoding='utf-8') as f:
        test_data = json.load(f)
    for i in test_data:
        test_case_list.append(tuple(i.values()))
    return test_case_list


# 第三方接口提取数据
def third_party(html):
    soup = BeautifulSoup(html, 'html.parser')
    list_data = {}
    for a in soup.find_all('input'):
        name = a['name']
        value = a['value']
        list_data.update({name: value})
    url = soup.form['action']
    response = requests.post(url, data=list_data)
    return response

















